<?php
/**
 * GioCMS
 * 
 * LICENSE
 *
 * This source file is subject to the GNU GENERAL PUBLIC LICENSE Version 2 
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://www.gnu.org/licenses/gpl-2.0.txt
 * If you did not receive a copy of the license and are unable to
 * obtain it through the world-wide-web, please send an email
 * to license@ninhgio.com so we can send you a copy immediately.
 * 
 * @copyright	Copyright (c) 2010-2011 GioCMS (http://cms.ninhgio.com)
 * @license		http://www.gnu.org/licenses/gpl-2.0.txt GNU GENERAL PUBLIC LICENSE Version 2
 * @author		NinhGio - ninhgio@gmail.com
 * @since		1.0
 */

class Modules_Core_Controllers_Locale extends Gio_Core_Controller
{
	public function configAction()
	{
		$request = $this->getRequest();
		
		$languagesFile = ROOT_DIR . DS . 'configs' . DS . 'languages.xml';
		if (!file_exists($languagesFile)) {
			return;
		}
		
		$languagesXml = @simplexml_load_file($languagesFile);
		if (null == $languagesXml) {
			return;
		}
		
		$locales          = array();
		foreach ($languagesXml->language as $language) {
			$arr 			  = explode('|', $language);
			$locales[$arr[0]] = array(
				'code'		  => $arr[0],
				'localName'   => $arr[1],
				'englishName' => $arr[2],
			);
		}
		$this->view->locales = $locales;
		$this->view->availableLanguages = array();
		$configs = Gio_Core_Config_Xml::getConfig();
		$this->view->configs = $configs;

		$default   = isset($configs->localization->languages->default)
						? $configs->localization->languages->default
						: $configs->web->language;
		$available = isset($configs->localization->languages->list)
						? explode(',', $configs->localization->languages->list)
						: array($default);
		$this->view->availableLanguages = $available;
		
		if ($request->isPost()) {
			$localizationEenable = ($request->getPost('localization_enable') != null) ? 'true' : 'false';
			$default   			 = $request->getPost('default_language');
			$available 			 = $request->getPost('supported_languages');
			$listLangs 			 = array($default);
			
			if ($available != null) {
				foreach ($available as $index => $locale) {
					if ($locale != $default) {
						$listLangs[] = $locale;
					}
				}
			}
			
			$configs->web->language 			   	   = $default;
			$configs->localization->enable 			   = $localizationEenable;
			$configs->localization->languages->default = $default;
			$configs->localization->languages->list    = implode(',', $listLangs);
			
			/**
			 * Reset language details
			 */
			$configs->localization->languages->details = null;
			
			foreach ($listLangs as $lang) {
				$configs->localization->languages->details->$lang = implode('|', array($locales[$lang]['code'], $locales[$lang]['localName'], $locales[$lang]['englishName'])); 
			}
			
			$configFile = Gio_Core_Config_Xml::getConfigFile();
			$content	= $configs->asXML();
			$fileName = Gio_Core_Config_Xml::getConfigFile();
			
			Gio_Core_File::writeToFile($fileName, $content);
			
			Gio_Core_Messenger::getInstance()
								->addMessage($this->view->TRANSLATOR->translator('locale_actions_config_success'));
			$this->redirect($this->view->url('core_locale_config'));
		}
	}
	
	public function viewhelperAction()
	{
		$this->setNoRender();
		$this->disableLayout();
		$request = $this->getRequest();
		if (!$request->isPost()) {
			return;
		}
		
		$id = $request->getPost('id');
		$lang = $request->getPost('language');
		$name = $request->getPost('name');
		$moduleId = $request->getPost('moduleId');
		$viewHelperAttributes = $request->getPost('viewHelperAttributes');
		$viewHelperClass = $request->getPost('viewHelperClass');
		$attributes = $request->getPost('viewHelperAttributes', '');
		
		$json = new Services_JSON();
		$attributes = (array)$json->decode($attributes);

		$response = call_user_func_array($viewHelperClass, array($attributes, $lang, $moduleId));
		$this->getResponse()->setBody($response);
	}
}